$(function() {
	getCookieValue("token",8888)
	//获取openid
	$('.price-right-one').on('tap', function() {
		var thisPrice = $('.vip-price-one').html();
		thisPrice = parseInt(thisPrice)*100;
		console.log(thisPrice)
		var newCode = CD.geturl().code;
		console.log(newCode);
		var addTo = "http://www.leyouhui888.cn/weChatPay/getAccessToken?code=" + newCode + "&state=STATE";
		vipPay(addTo,thisPrice);
	});
	$('.price-right-two').on('tap', function() {
		var thisPrice = $('.vip-price-two').html();
		thisPrice = parseInt(thisPrice)*100;
		console.log(thisPrice)
		var newCode = CD.geturl().code;
		console.log(newCode);
		var addTo = "http://www.leyouhui888.cn/weChatPay/getAccessToken?code=" + newCode + "&state=STATE";
		vipPay(addTo,thisPrice);
	});

});

var payTo = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/weChatPay/userPay",
		data: params,
		dataType: 'json',
		success: function(data) {
			console.log(data);
			callback && callback(data)
		}
	});
}

var vipPay = function(addTo,thisPrice) {
	$.ajax({
		type: "get",
		url: addTo,
		data: '',
		dataType: 'json',
		success: function(data) {
			console.log(data);
			var getOpenId = data.data.openid;
			console.log(getOpenId);
			payTo({
				token: getCookieValue("token"),
				price: thisPrice,
				openId: getOpenId,
			}, function(data) {
				console.log(data);
				var oneTo = data.appId;
				var twoTo = data.timeStamp;
				var threeTo = data.nonceStr;
				var fourTo = data.package;
				var fiveTo = data.signType;
				var sixTo = data.paySign;
				console.log(fourTo);

				//调微信支付
				WeixinJSBridge.invoke('getBrandWCPayRequest', {
					"appId": oneTo,
					"timeStamp": twoTo,
					"nonceStr": threeTo,
					"package": fourTo,
					"signType": fiveTo,
					"paySign": sixTo,
					"jsApiList": ['checkJsApi', 'startRecord', 'stopRecord', 'translateVoice', 'scanQRCode', 'openCard']
				}, function(res) {
					if(res.err_msg == "get_brand_wcpay_request:ok") {
						alert('支付成功');
						//支付成功后跳转的页面
						window.location.href = 'l-userlogin.html'
					} else if(res.err_msg == "get_brand_wcpay_request:cancel") {
						alert('支付取消');
					} else if(res.err_msg == "get_brand_wcpay_request:fail") {
						alert('支付失败');
						WeixinJSBridge.call('closeWindow');
					} //使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
				})
			});

		}
	});
}